package com.example.system.controller;

import com.example.system.common.Result;
import com.example.system.dto.SleepDuration;
import com.example.system.entity.Health;
import com.example.system.entity.Sleep;
import com.example.system.exception.CustomException;
import com.example.system.service.impl.SleepServiceImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/sleep")
@MapperScan("com.*.mapper")
public class SleepController {
    @Autowired
    SleepServiceImpl sleepService;

    // 根据用户id查询睡眠信息
    @GetMapping("/search/{userId}")
    public Result findSleepByUserId(@PathVariable Integer userId) {
        List<SleepDuration> sleepList = sleepService.findSleepByUserId(userId);
        if (sleepList == null || sleepList.isEmpty()) {
            return Result.error("尚未检测过当前用户的睡眠信息");
        }
        return Result.success(sleepList);
    }

    // 添加睡眠信息
    @PostMapping("/add")
    public Result addBySleep(@RequestBody Sleep sleep) {
        try {
            sleepService.addSleepBySleep(sleep);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success();
    }

    // 根据睡眠id删除睡眠信息
    @DeleteMapping("/delete/{sleepId}")
    public Result deleteBySleepId(@PathVariable Integer sleepId) {
        Integer deleteRes = sleepService.deleteSleepBySleepId(sleepId);
        return Result.success(deleteRes);
    }
}
